﻿/*
 * This code is part of cav94mat libraries.
 * (c) 2013, cav94mat - http://www.cav94mat.com/
 *
 *  -- Distributed under terms of the Simplified BSD License --
 * 
 *  Redistribution and use in source and binary forms, with or without modification, are permitted
 *  provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice, this list of conditions
 *    and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 * 
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 *  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
 *  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 *  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 *  POSSIBILITY OF SUCH DAMAGE.
 * 
 */
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
namespace cav94mat
{
    public static class ConsoleUtils
    {
        public static void ExplodeCmdArgs(string fullargs, ref Collection<string> positionals, ref Collection<string> flags, ref Dictionary<string, string> props)
        {
            fullargs = fullargs.Trim().Trim('\r').Trim('\n');
            //On Error Resume Next
            // Match all double-quoted fields
            const string pattern = "\"(.*?)\"";
            MatchCollection col = Regex.Matches(fullargs, pattern);
            // Copy groups to a string[] array
            string[] fields = new string[col.Count];
            for (int i = 0; i <= fields.Length - 1; i++)
            {
                // (Index 1 is the first group)
                fields[i] = col[i].Groups[1].Value;
            }
            uint subtract = 0;
            foreach (Match m in col)
            {
                string @out = "";
                if (m.Index > 0)
                    @out = fullargs.Substring(0, (int)(m.Index - subtract));
                string ph = "\"{" + Array.IndexOf(fields, m.Groups[1].Value) + "}\"";
                @out += ph;
                @out += fullargs.Substring((int)((m.Index - subtract) + m.Length));
                subtract += (uint)(m.Groups[1].Value.Length + 2 - ph.Length);
                fullargs = @out;
                @out = "";
                //aaaXXXXXyy
                //0123456789
                //‾  ‾    ‾
            }
            string[] args = fullargs.Split(" ".ToCharArray());
            for (int i = 0; i <= args.Length - 1; i++)
            {
                for (int k = 0; k <= fields.Length - 1; k++)
                {
                    args[i] = args[i].Replace("\"{" + k + "}\"", fields[k]);
                }
            }
            ExplodeCmdArgs(args, ref positionals, ref flags, ref props);
        }
        public static void ExplodeCmdArgs(string[] fullargs, ref Collection<string> positionals, ref Collection<string> flags, ref Dictionary<string, string> props)
        {
            positionals = new Collection<string>();
            flags = new Collection<string>();
            props = new Dictionary<string, string>();

            foreach (string j in fullargs)
            {
                if (j.StartsWith("--"))
                {
                    flags.Add(j.Substring(2));
                }
                else if (j.StartsWith("-"))
                {
                    string a = j.Substring(1);
                    KeyValuePair<string, string> x = default(KeyValuePair<string, string>);
                    if (a.Contains("="))
                    {
                        x = new KeyValuePair<string, string>(a.Split("=".ToCharArray(), 2)[0], a.Split("=".ToCharArray(), 2)[1]);
                    }
                    else if (a.Contains(":"))
                    {
                        x = new KeyValuePair<string, string>(a.Split(":".ToCharArray(), 2)[0], a.Split(":".ToCharArray(), 2)[1]);
                    }
                    else
                    {
                        x = new KeyValuePair<string, string>(a, "yes");
                        if (ImplicitBooleanPropsAsFlags)
                            flags.Add(a);
                    }
                    props.Add(x.Key, x.Value);
                }
                else
                {
                    positionals.Add(j);
                }
            }
            GC.Collect();
        }
        public static bool ImplicitBooleanPropsAsFlags { get; set; }
    }
}